package com.chunyu.app.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;

public class JsonUtils {

	private final static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
	private static final List<SerializerFeature> featureslist = new ArrayList<SerializerFeature>();
	private static final SerializerFeature[] features;
	private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
	private static final Charset CHARSET = Charset.forName("UTF-8");
	private static final FastJsonConfig config = new FastJsonConfig();
	static {
		featureslist.add(SerializerFeature.PrettyFormat);
		featureslist.add(SerializerFeature.WriteMapNullValue);
		featureslist.add(SerializerFeature.QuoteFieldNames);
		featureslist.add(SerializerFeature.WriteNullListAsEmpty);
		featureslist.add(SerializerFeature.WriteNullStringAsEmpty);
		featureslist.add(SerializerFeature.WriteDateUseDateFormat);
		featureslist.add(SerializerFeature.WriteNullNumberAsZero);
		featureslist.add(SerializerFeature.WriteNullBooleanAsFalse);
		featureslist.add(SerializerFeature.DisableCircularReferenceDetect);
		featureslist.add(SerializerFeature.SkipTransientField);
		featureslist.add(SerializerFeature.NotWriteRootClassName);
		features = new SerializerFeature[featureslist.size()];
		featureslist.toArray(features);
		config.setCharset(CHARSET);
		config.setDateFormat(dateFormat);
		config.setWriteContentLength(true);
		config.setSerializerFeatures(features);
	}

	public static String toJson(Object obj) {
		if (NullUtils.isEmpty(obj)) {
			return null;
		}
		try {
			return JSONObject.toJSONStringWithDateFormat(obj, dateFormat, features);
		} catch (Exception e) {
			throw new RuntimeException("转换json字符失败!");
		}
	}

	public static byte[] toJsonBytes(Object obj) {
		if (NullUtils.isNull(obj)) {
			return null;
		}
		try {
			return JSONObject.toJSONString(obj).getBytes("UTF-8");
		} catch (Exception e) {
			logger.error("toJsonBytes", e);
			throw new RuntimeException("转换json字符失败!");
		}
	}

	public static <T> T toObject(String json, Class<T> clazz) {
		if (NullUtils.isEmpty(json)) {
			return null;
		}
		return JSONObject.parseObject(json, clazz);
	}

	public static <T> T toObject(byte[] jsonString, Class<T> clazz) throws Exception {
		if (NullUtils.isEmpty(jsonString)) {
			return null;
		}
		try {
			String json = new String(jsonString, "UTF-8").trim();
			return JSONObject.parseObject(json, clazz);
		} catch (Exception e) {
			logger.error("toObject", e);
			throw e;
		}
	}

	public static <T> List<T> toObjectArray(String jsonString, Class<T> clazz) {
		if (NullUtils.isEmpty(jsonString)) {
			return new ArrayList<T>();
		}
		return JSONArray.parseArray(jsonString, clazz);

	}

	public static <T> List<T> toObjectArray(byte[] jsonString, Class<T> clazz) throws Exception {
		if (NullUtils.isEmpty(jsonString)) {
			return new ArrayList<T>();
		}
		try {
			return JSONArray.parseArray(new String(jsonString, "UTF-8"), clazz);
		} catch (UnsupportedEncodingException e) {
			throw e;
		}

	}

	public static final FastJsonConfig getFastJsonConfig() {
		return config;
	}
}
